

require 'fileutils'
require 'CSV'

module OPayPlugin

class OPayPluginXCConfig
  GZCONFIG_STD_CONFIG_LIST ||= %w[public.xcconfig core.xcconfig].freeze
  GZCONFIG_GLB_CONFIG_LIST ||= %w[version.xcconfig].freeze
  GZCONFIG_EXT_CONFIG_FILE ||= 'Pods/extend.xcconfig'.freeze
  GZCONFIG_PACKAGE_INFO_FILE ||= '/Pods/packageinfo'.freeze
  CONFIG_REPLACEMENT_PACK_PATH ||= 'env_replacement'.freeze
  CONFIG_PACK_PATH ||= 'env'.freeze
  CONFIG_DIR_NAME ||= 'config'.freeze

  def self.prepare_config_with_name(pack_name)

    config_list = []

    working_path = OPayXCConfigPlugin::OPayXCConfigContext.instance.working_path
    raise "do not have enouth access on path:#{working_path}" unless File.writable? working_path

    replacement_path = File.join(working_path, CONFIG_REPLACEMENT_PACK_PATH)
    if File.directory? replacement_path
      raise "unreadable file :#{replacement_path}" unless File.readable? replacement_path
      pack_path = replacement_path
    else
      pack_path = File.join(working_path, CONFIG_PACK_PATH, pack_name)
    end

    config_file_dir = File.join(working_path, CONFIG_DIR_NAME)

    puts '=============================================='
    puts __dir__
    puts "pack file path:#{pack_path}"
    puts "config file path:#{config_file_dir}"
    puts '=============================================='
    # 删除旧版本信息
    `rm -r -f #{config_file_dir}`
    # 把配置信息移动到 config_file_dir
    FileUtils.ln_s(pack_path, config_file_dir, force: true)
    # 读取pubic、core.xcconfig
    config_list.concat(GZCONFIG_STD_CONFIG_LIST.map { |cfg|
      File.join(pack_path, cfg)
    })
    # 读取git配置信息
    ext_cfg_path = File.join(working_path, GZCONFIG_EXT_CONFIG_FILE)
    `rm -r -f #{ext_cfg_path}`

    package_builder = `git config user.name`.strip
    package_builder = `whoami` if package_builder.empty?
    package_env = pack_name # 这个仅用于输出到packageinfo文件

    hash = `git rev-parse --short HEAD`.strip
    hash = rand(0xffffff) if hash.start_with?('fatal') || hash.empty?

    branch = `git symbolic-ref --short -q HEAD`.strip
    branch = rand 0xffffff if branch.start_with?('fatal') || branch.empty?

    package_id = "#{branch}_#{hash}"
    package_desc = ''


    package_info_path = File.join(working_path, GZCONFIG_PACKAGE_INFO_FILE)
    if File.readable? package_info_path
      CSV.foreach(File.join(working_path, GZCONFIG_PACKAGE_INFO_FILE)) do |row|

        next if row[0].nil? || row[1].nil?

        key = row[0].strip
        val = row[1].strip

        case key
        when 'GZ_PACKAGE_ID'
          package_id = val unless val.empty?
        when 'GZ_PACKAGE_DESC'
          package_desc = val unless val.empty?
        when 'GZ_PACKAGE_BUILDER'
          package_builder = val unless val.empty?
        when 'GZ_PACKAGE_BRANCH'
          branch = val unless val.empty?
        when 'GZ_PACKAGE_ENV'
          package_env = val unless val.empty?
        else
          puts "unknown package info type:#{key}"
        end

      end
    end


    File.open(ext_cfg_path, 'w+') do |file|
      file.puts "GZ_PACKAGE_ENV |= \\@\\\"#{package_env}\\\""
      file.puts "GZ_PACKAGE_ID |= \\@\\\"#{package_id}\\\""
      file.puts "GZ_PACKAGE_DESC |= \\@\\\"#{package_desc}\\\""
      file.puts "GZ_CODE_IDENTIFIER |= \\@\\\"#{branch}_#{hash}\\\""
    end

    puts ">>>>>>>>>>>>>" + ext_cfg_path

    config_list.push ext_cfg_path

    puts "=============="
    puts  config_list
    puts "=============="

    OPayXCConfigPlugin::OPayXCConfigContext.instance.configfile_list = config_list
    OPayXCConfigPlugin::OPayXCConfigContext.instance.environment_name = pack_name

  end
end
end
